<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="50%"
    :minHeight="300"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchemaAction } from './menu.data';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { BasicModal, useModalInner } from '/@/components/Modal';

  // import {
  //   updateUsinghandledefAuthApi,
  //   addUsinghandledefAuthApi,
  //   UsinghandledefAuthInfoApi,
  // } from '/@/api/admin/auth';
  // import { object } from 'vue-types';

  export default defineComponent({
    name: 'ActionAuthModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      let editData: any = {};
      let emitData;
      const { createMessage } = useMessage();
      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 90,
        baseColProps: { span: 12 },
        schemas: formSchemaAction,
        showActionButtonGroup: false,
      });
      const [registerDrawer, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });

        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          // let fieldSetting = ['0', '0', '0', '0'];
          let fieldSetting = ['0', '0', '0'];
          fieldSetting[0] =
            data.record.pkey == 1 || data.record.pkey == 'true' || data.record.pkey == true
              ? '1'
              : '0';
          // fieldSetting[1] = data.record.isnullable === 1 ? '2' : '';
          //
          fieldSetting[1] =
            data.record.isnullable == 1 ||
            data.record.isnullable == 'true' ||
            data.record.isnullable == true
              ? '3'
              : '0';
          fieldSetting[2] =
            data.record.isUnique == 1 ||
            data.record.isUnique == 'true' ||
            data.record.isUnique == true
              ? '4'
              : '0';

          // fieldSetting[2] = data.record.isUnique == 1 ? '4' : '0';
          // fieldSetting[3] = '4';
          data.record.fieldSetting = fieldSetting;
          console.log(' data.record：>>>', data.record);
          // data?.record.isnullable === 3 ? 1 : 0
          // 是否主键1  是否外键2 isnullable 3是否可为空 isUnique 4是否唯一标识

          setFieldsValue(data.record);
          emitData = data.record;
          // const ret = await UsinghandledefAuthInfoApi(data.record.handleId);
          // editData = ret || {};
          // setFieldsValue({
          //   ...ret,
          // });
        } else {
          emitData = {};
          editData.moduleId = data.moduleId;
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '添加操作' : '编辑操作'));

      async function handleSubmit() {
        try {
          const values = await validate();
          // return;
          // TODO custom api
          setModalProps({ confirmLoading: true });

          // const params = Object.assign(editData, values);
          // const apiFn = unref(isUpdate) ? updateUsinghandledefAuthApi : addUsinghandledefAuthApi;
          // await apiFn(params);
          // updatePost
          closeModal();
          createMessage.success('操作成功');
          const param = Object.assign(emitData, values);
          console.log('参数2@@@@@：', param);
          // param.isnullable = param.fieldSetting[0] === '1' ? 1 : 0;
          // param.isnullable = param.fieldSetting[1] === '2' ? 1 : 0;
          //缺少 1  2
          param.pkey = false;
          param.isnullable = false;
          param.isUnique = false;
          param?.fieldSetting?.forEach((e) => {
            switch (e) {
              case '1':
                param.pkey = true;
                // param.isnullable = param.fieldSetting[0] === '1' ? 1 : 0;
                break;
              case '2':
                // param.isnullable = param.fieldSetting[1] === '2' ? 1 : 0;
                break;
              case '3':
                param.isnullable = true;
                break;
              case '4':
                param.isUnique = true;
                break;
              default:
            }
          });
          // param.isnullable = param.fieldSetting[2] === '3' ? 1 : 0;
          // param.isUnique = param.fieldSetting[3] === '4' ? 1 : 0;
          emit('success', param, isUpdate);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }
      return {
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
